package com.example.todolist.view

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.todolist.database.model.CategoryEntity
import com.example.todolist.databinding.FragmentCategroyBinding
import com.example.todolist.util.LoggerTag.Companion.MAIN_CATEGORY
import com.example.todolist.view.dialog.AddItemDialogFragment
import com.example.todolist.viewmodel.CategoryDetailViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class CategoryActivity : AppCompatActivity() {
    private lateinit var binding: FragmentCategroyBinding
    private lateinit var adapter: CategoryfragAdapter
    private lateinit var recyclerView: RecyclerView
    private val viewmodel: CategoryDetailViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FragmentCategroyBinding.inflate(layoutInflater)
        setContentView(binding.root)
//// 获取Application作用域的ViewModel
//        viewmodel = ViewModelProvider(
//            this,
//            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
//        )[CategoryDetailViewModel::class.java]
        // 初始化 RecyclerView
        recyclerView = binding.categoryDetail
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = CategoryfragAdapter(
            exitDelMode = {
                exitDeleteMode()
            },
            startDelMode = {
                enterDeleteMode()
            },
        )
        recyclerView.adapter = adapter

        // 获取分类列表并观察
        viewmodel.cDetails.observe(this) {
            adapter.submitList(it)
            Log.d(MAIN_CATEGORY, "onCreate:${viewmodel.cDetails.value} ")
        }
        // 新增
        binding.addCategory.setOnClickListener {
            binding.addCategory.visibility = View.GONE
            addCategory(it)
        }
        // 取消删除模式
        binding.cancelButton.setOnClickListener {
            exitDeleteMode()
        }
        // 删除
        binding.deleteButton.setOnClickListener {
            deleteSelectedItems()
        }
    }

    private fun addCategory(view: View) {
        val dialog = AddItemDialogFragment(
            onItemAdded = { name ->
                viewmodel.insertCategory(CategoryEntity(label = name))
                Log.d(MAIN_CATEGORY, "addCategory: 加入数据库！！")
            },
            onAddBtn = {
                binding.addCategory.visibility = View.VISIBLE
            },
        )
        dialog.show(supportFragmentManager, "AddItemDialog")
    }

    private fun enterDeleteMode() {
        binding.multiSelectToolbar.visibility = View.VISIBLE
        binding.addCategory.visibility = View.GONE
        viewmodel.initIsSelected() // 重置选中状态
    }

    private fun exitDeleteMode() {
        binding.multiSelectToolbar.visibility = View.GONE
        binding.addCategory.visibility = View.VISIBLE
        adapter.exitAdapterDelMode()
    }

    private fun deleteSelectedItems() {
        val selectedItems = adapter.getSelectedItems()
        viewmodel.deleteCategories(selectedItems)
        adapter.initSelectedItems()
        exitDeleteMode()
    }
}

